import type { TreeItem } from "@/components/Tree";
import { BasicTree } from "@/components/Tree";
import { listSimpleDept } from "@/api/system/dept";
import { handleTree } from "@/utils/tree";
import { useEffect, useState } from "react";

const DeptTree = (props: { select: Fn } & any) => {
	const { select } = props;

	const [treeData, setTreeData] = useState<TreeItem[]>([]);

	async function fetch() {
		const res = await listSimpleDept();
		setTreeData(handleTree(res, "id"));
	}
	useEffect(() => {
		fetch();
	}, []);

	return (
		<div className="m-4 mr-0 overflow-hidden">
			<BasicTree
				treeWrapperClassName={"h-[calc(100%-35px)] overflow-auto"}
				clickRowToExpand={false}
				treeData={treeData}
				fieldNames={{ key: "id", title: "name" }}
				updateSelect={select}
			/>
		</div>
	);
};

export default DeptTree;
